//
//  VerifyViewController.m
//  YTwallet
//
//  Created by ZhangLin on 2019/3/19.
//  Copyright © 2019年 hadrlog. All rights reserved.
//

#import "VerifyViewController.h"
#import "VerifyView.h"
#import "GestureViewController.h"
#import "PCCircleViewConst.h"
@interface VerifyViewController ()

@property(nonatomic,strong)VerifyView *vv;

@end

@implementation VerifyViewController

-(void)loadView
{
    [super loadView];
    self.vv = [[VerifyView alloc] initWithFrame:self.view.bounds];
    self.view = self.vv;
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    NSString *str = [AppTools shareInstance].userInfo.loginName;
    if (str.length == 11) {
       self.vv.phoneNumLab.text = [self changeTelephone:str];
    }
}
//手机号隐藏中间4位
-(NSString *)changeTelephone:(NSString *)teleStr
{
    NSString * string = [teleStr stringByReplacingOccurrencesOfString:[teleStr substringWithRange:NSMakeRange(3,4)]withString:@"****"];
    return string;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"验证身份";
    self.view.backgroundColor = [UIColor colorWithRed:242 / 255.0 green:244 / 255.0 blue:246 / 255.0 alpha:1];
    [self.vv.loginBtn addTarget:self action:@selector(loginBtnAction) forControlEvents:(UIControlEventTouchUpInside)];
    
    [self.vv.loginPwdTf addTarget:self action:@selector(textFieldDidChange:) forControlEvents:(UIControlEventEditingChanged)];
}

//输入框符合规则可以点击"下一步"
-(void)textFieldDidChange :(UITextField *)textField{
    if (self.vv.loginPwdTf.text.length > 0 ) {
        //验证码输入的是6位  手机号输入框是手机号
        self.vv.loginBtn.userInteractionEnabled = YES;
        self.vv.loginBtn.backgroundColor = ETCONFIRMCOLOR;
    }else
    {
        self.vv.loginBtn.userInteractionEnabled = NO;
        self.vv.loginBtn.backgroundColor = ETCOMMONCOLOR;
    }
}



-(void)loginBtnAction
{
    
    LoginRequest *request = [[LoginRequest alloc] init];
    request.loginName = [AppTools shareInstance].userInfo.loginName;
    request.passWord = [self.vv.loginPwdTf.text md5];
    NSDictionary *dic = [request mj_keyValues];
    NSString *url = [[ServerAddressModel shareInstance] fullTAnd0AddrWithParameter:@"/epay_apps/login/loginA0100"];
    __weak __typeof(self) weakSelf = self;
    [[ETONEHttpClient sharedInstance] doHTTPSPOST:url Parameters:dic HttpBody:nil SuccessHandler:^(id responseObject) {
        
        NSDictionary *dic = [responseObject mj_JSONObject];
        NSString *code = dic[@"code"];
        if ([@"0000" isEqualToString:code]) {
            [weakSelf loginSuccessWithResponse:responseObject];
        }
    } FailedHandler:^(NSError *error) {
        
        NSLog(@"%@",error.localizedDescription);
    }];
    
}
//登录成功跳转
-(void)loginSuccessWithResponse:(id )responseObject
{
    //验证密码成功之后清空手势   跳转到设置
    [PCCircleViewConst saveGesture:@"" Key:gestureFinalSaveKey];
    
    [self showSuccessResultTips:@"身份验证成功" animated:YES forSeconds:SUCCESS_SHOW_TIME_DEF completionBlock:^{
        
        GestureViewController *gestureVC = [[GestureViewController alloc] init];
        gestureVC.type = GestureViewControllerFromResetVC;
        [self.navigationController pushViewController:gestureVC animated:YES];
        
    }];
}


@end
